package cn.lyq.card.ui.qualified

import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.DeviceInfo
import cn.lyq.card.data.src.Result
import cn.lyq.card.data.src.Status
import cn.lyq.card.utils.bindViewPager
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.setSimpleData
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_qualified_device.*
import kotlinx.android.synthetic.main.layout_expend_type.*
import kotlinx.android.synthetic.main.layout_tab_normal.*


class QualifiedDeviceActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(CashBackVM::class.java)
    }
    private val mPageAdapter by lazy {
        PagerAdapter(this)
    }
    private val mTypeAdapter by lazy {
        DeviceAdapter()
    }

    override fun getLayoutId(): Int {
        return R.layout.act_qualified_device
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_qualified_statistic)
        groupExpendType.visibility = View.GONE

        val tabTitles = arrayOf("押金版", "非押版")
        tabLayout.setSimpleData(tabTitles)
        viewPager.adapter = mPageAdapter
        tabLayout.setSimpleData(tabTitles)
        tabLayout.bindViewPager(viewPager)

        rvExpendType.layoutManager = GridLayoutManager(this, 3)
        rvExpendType.adapter = mTypeAdapter
    }

    override fun initListener() {
        super.initListener()
        tvType.setClick {
            groupExpendType.visibility = View.VISIBLE
        }
        groupExpendType.setClick {
            groupExpendType.visibility = View.GONE
        }
        tvType.setClick {
            if (mTypeAdapter.data.size == 0) {
                showLoading()
                getBillType()
            } else {
                groupExpendType.visibility = View.VISIBLE
            }
        }
        mTypeAdapter.setOnItemClickListener { _, _, position ->
            tvType.text = mTypeAdapter.data[position].title
            mModel.goodsId.postValue(mTypeAdapter.data[position].goodsId)
            groupExpendType.visibility = View.GONE
        }
    }

    override fun initObserver() {
        super.initObserver()
        mModel.goodsId.postValue(0)
    }


    private fun getBillType() {
        mModel.getDeviceList().observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> getBillTypeSuccess(it)
                Status.ERROR -> {
                    dismissLoading()
                    ToastUtils.showShort(it.message.getMsg())
                }
                else -> {
                }
            }
        })
    }

    private fun getBillTypeSuccess(it: Result<MutableList<DeviceInfo>>) {
        mTypeAdapter.setList(it.requireData().apply {
            add(0, DeviceInfo(0, "全部机型"))
        })
        groupExpendType.visibility = View.VISIBLE
        dismissLoading()
    }

    private class PagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

        override fun getItemCount(): Int = 2

        override fun createFragment(position: Int): CashBackStageFg {
            return CashBackStageFg.newInstance(position)
        }

    }
}